    //
//  addressBookViewController.m
//  Wtracker
//
//  Created by Anna on 7/4/10.

#import "addressBookViewController.h"
#import "WtrackerAppDelegate.h"

#define BARBUTTON(TITLE, SELECTOR) [[[UIBarButtonItem alloc] initWithTitle:TITLE style:UIBarButtonItemStylePlain target:self action:SELECTOR] autorelease]

#define SECTIONTITLE  @"Preferred default email"

@implementation addressBookViewController

@synthesize getContactButton, myselfButton,createNewContactButton, selectedEmail, myDict;

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)viewDidLoad {
    [super viewDidLoad];
	NSLog(@"inside abvc");

	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	myDict = appDelegate.myDict;

	self.navigationItem.title = SECTIONTITLE;
	self.navigationItem.rightBarButtonItem = BARBUTTON(@"Save", @selector(saveSettings));

	selectedEmail.text = [myDict objectForKey:SECTIONTITLE];

}

#pragma mark custom methods
-(IBAction)getContact{
		ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
		picker.peoplePickerDelegate = self;
		// Display only a person's phone, email, and birthdate
		NSArray *displayedItems = [NSArray arrayWithObjects:[NSNumber numberWithInt:kABPersonPhoneProperty], 
								   [NSNumber numberWithInt:kABPersonEmailProperty],
								   [NSNumber numberWithInt:kABPersonBirthdayProperty], nil];
		
		
		picker.displayedProperties = displayedItems;
		// Show the picker 
		[self presentModalViewController:picker animated:YES];
		[picker release];	
}

-(IBAction)newContact{
		ABNewPersonViewController *picker = [[ABNewPersonViewController alloc] init];
		picker.newPersonViewDelegate = self;
		
		UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:picker];
		[self presentModalViewController:navigation animated:YES];
		
		[picker release];
		[navigation release];	
}

-(IBAction)selectMe{
	selectedEmail.text = [myDict objectForKey:@"Your Email"];
}

- (BOOL)peoplePickerNavigationController: (ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
	
	ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonEmailProperty);
	int count = ABMultiValueGetCount(multi);
	if(count > 0){
		selectedEmail.text = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
	} 
		
    [self dismissModalViewControllerAnimated:YES];
	
    return NO;
}

- (void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker;
{
	[self dismissModalViewControllerAnimated:YES];
}

// Does not allow users to perform default actions such as dialing a phone number, when they select a person property.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person 
								property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifier
{
	return NO;
}

- (BOOL)personViewController:(ABPersonViewController *)personViewController shouldPerformDefaultActionForPerson:(ABRecordRef)person 
					property:(ABPropertyID)property identifier:(ABMultiValueIdentifier)identifierForValue
{
	return NO;
}



#pragma mark custom methods
-(IBAction)saveSettings{
	[myDict setObject:selectedEmail.text forKey:SECTIONTITLE];
	WtrackerAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
	appDelegate.myDict = myDict;	
	[appDelegate saveSettings];
	myDict = appDelegate.myDict;
	
	[self.navigationController popViewControllerAnimated:YES];
}


-(void)showAlert:(NSString *)theMessage{
	UIAlertView *infoAlert = [[[UIAlertView alloc] initWithTitle:@"Error" message:theMessage delegate:self cancelButtonTitle:@"OK" 
											   otherButtonTitles:nil] autorelease];
	[infoAlert show];
	
	
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[getContactButton release];
	[myselfButton release];
	[createNewContactButton release];

    [super dealloc];
}


@end
